package dijkstra;

import pacman.cpac;

public class Vertex implements Comparable<Vertex>
{	
	public static final int DOTS=1,PACMAN=2,EDGE=3;
	public int tipo;
	public int x,y;
    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { name = argName; }
    public int provenienteDa;
    public Vertex(int x, int y){
    	this("x="+x+" y="+y);
    	tipo=EDGE;
    	this.x=x;
    	this.y=y;
    }
    public Vertex clone(){
    	Vertex ret= new Vertex(x, y);
    	return ret;
    	
    }
    public posizione dammiPosizione(){
    	return new posizione(x,y);
    }
    public String toString() { 
    	return name; 
    }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }
	public void addEdge(Vertex init, int distanza) {
		if(adjacencies==null){
			adjacencies= new Edge[1];
			adjacencies[0]= new Edge(init, distanza);
		}
		else{
			for(int i=0;i<adjacencies.length;i++){
				if(adjacencies[i].target==init){
					//System.err.print("Sostituisco");
					adjacencies[i].weight=distanza;
					return;
				}
			}
			Edge[] nuovo= new Edge[adjacencies.length+1];
			for(int i=0;i<adjacencies.length;i++){
				nuovo[i]=adjacencies[i];
			}
			nuovo[adjacencies.length]=new Edge(init, distanza);
			adjacencies=nuovo;
		}
		
	}
	public boolean raggiunto(int i, int j) {
		return Math.abs(i-x)<1&&Math.abs(j-y)<1;
		
	}
	public boolean raggiunto(cpac pacman2) {
		int posx=Math.round(((float)pacman2.iX+4)/16);
		int posy=Math.round(((float)pacman2.iY+4)/16);
		return raggiunto(posy,posx);
	}
	public boolean equals(Object object){
		if(object instanceof Vertex){
			return x==((Vertex) object).x&&y==((Vertex) object).y;
		}
		return false;
	}
	public Edge dammiVerticeCheVaA(Vertex dest) {
		for(int i=0;i<adjacencies.length;i++){
			if(adjacencies[i].target.equals(dest)){
				return adjacencies[i];
			}
		}
		return null;
	}
	public void removeEdge(Vertex scollega) {
		if(adjacencies==null){
			return;
		}
		Edge[] nuovi= new Edge[adjacencies.length-1];
		int sum=0;
		for(int i=0;i<adjacencies.length&&sum<nuovi.length;i++){
			
			if(!adjacencies[i].target.equals(scollega)){
				nuovi[sum++]=adjacencies[i];
			}
		}
		if(sum==nuovi.length){
			adjacencies=nuovi;
			//System.out.println("Rimuovi! :D");
		}
		
	}
}
